#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

/*
	./led_test /dev/led_drv on
	./led_test /dev/led_drv off
	./led_test /dev/led_drv
*/

int main(int argc, char *argv[])
{
    int len;
    char buf[10];

    if(argc < 2){
        printf("please input  at least 2 args\n");
        printf("%s /dev/led_drv [on/off]\n", argv[0]);
        return -1;
    }

    /*open*/
    int fd;
    fd = open(argv[1], O_RDWR);
    if(fd < 0){
        printf("open failed\n");
        return -2;
    }

    /*read led*/
    if(argc == 2){  
        int res = read(fd, buf, 1);
        printf("led state : %s \n", buf[0] == 1 ? "off" : "on");
    }

    /*write led*/
    if(argc == 3){
		if(strcmp(argv[2], "on") == 0)
			buf[0] = 0;
		else if(strcmp(argv[2], "off") == 0)
			buf[0] = 1;
		write(fd, buf, 1);
    }

    close(fd);

    return 0;
}
